package com.lanmon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lanmon.entity.ComUser;
import com.lanmon.springmvc.annotation.bind.RequestJsonParam;
import com.lanmon.springmvc.wapper.MapWapper;


@Controller
@RequestMapping("/jsonparam")  
public class JsonParamController {

    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,34]
    //fail http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,a]
    @RequestMapping("/list")  
    public String list(@RequestJsonParam("list") List<Integer> list) {
        System.out.println(list);
        return "redirect:/success";        
    }
    
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,34]
    //fail http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,a]
    @RequestMapping("/set")  
    public String set(@RequestJsonParam("set") Set<Integer> set) {
        System.out.println(set);
        return "redirect:/success";        
    }
    
    
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,3]
    //fail http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,a]
    @RequestMapping("/array")  
    public String list(@RequestJsonParam("array") int[] array) {
        System.out.println(Arrays.toString(array));
        return "redirect:/success";        
    }
    
    
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":2}
    //fail http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":a}
    @RequestMapping("/map")  
    public String map(@RequestJsonParam(value = "map", required=false) MapWapper<String, Integer> map) {
        System.out.println(map.get("a"));
        return "redirect:/success";        
    }
    
    
    //ComUser[]
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/array2?array=[{"username":"123"},{"username":"234"}]
    @RequestMapping("/array2")  
    public String array2(@RequestJsonParam(value = "array") ComUser[] array) {
        System.out.println(Arrays.toString(array));
        return "redirect:/success";        
    }

    //List<ComUser>
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/list2?list=[{"username":"123"},{"username":"234"}]
    @RequestMapping("/list2")  
    public String list2(@RequestJsonParam(value = "list") List<ComUser> list) {
        System.out.println(list);
        return "redirect:/success";        
    }

    //Set<ComUser>
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/set2?set=[{"username":"123"},{"username":"234"}]
    @RequestMapping("/set2")  
    public String set2(@RequestJsonParam(value = "set") Set<ComUser> set) {
        System.out.println(set);
        return "redirect:/success";        
    }
    
    //Map<String, ComUser>
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/map2?map={"a":{"username":"123"},"b":{"username":"234"}}
    //暂不支持 Map<ComUser, ComUser>
    @RequestMapping("/map2")  
    public String map2(@RequestJsonParam(value = "map") MapWapper<String, ComUser> map) {
        System.out.println(map);
        return "redirect:/success";        
    }
    
    
    //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname":"zhang","workInfo":{"city":"abc","job":"abc","year":"abc"}, "schoolInfo":{"schoolType":"1","schoolName":"1","specialty":"1"}}
    //没有realname1
    //fail http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname1":123}
    @RequestMapping("/model1")  
    public String model1(@RequestJsonParam(value = "model", required=true) ComUser user) {
        System.out.println(user);
        return "redirect:/success";        
    }
    
    
    

}
